<?php
/**
 * Created by PhpStorm.
 * User: tom_xia
 * Date: 2018/4/25
 * Time: 18:53
 * 写日志
 */
namespace app\cls\tom\log;
use app\cls\tom\Tools;

abstract class LogBase
{
    public $msg = "";
    public $detail = "";
    public $type = 1; //1 普通消息
    public $key;//消息关键字，用户搜索

    public function __construct(array $config)
    {
        foreach($config as $attr=>$value){
            $this->$attr = $value;
        }
    }

    /**
     * 写日志
     * @return mixed
     */
    abstract public function write();

    public function getKeyWordMsg()
    {
        $debugInfo = debug_backtrace();
        $keyword = pathinfo($debugInfo[count($debugInfo)-1]["file"],PATHINFO_FILENAME);
        if(empty($this->key)){
            return $keyword;
        }else{
            return "{$this->key}";
        }
    }
    public function getDetail()
    {
        if(is_array($this->detail)){
            $this->detail["server_ip"] = Tools::get_client_ip(false);
            return json_encode($this->detail,JSON_UNESCAPED_UNICODE);
        }else{
            return $this->detail." server_ip:".Tools::get_client_ip(false);
        }
    }
}